package mr

import (
	"errors"
	"time"
)

type TaskState int

const (
	TaskWaiting = iota
	TaskWorking
	TaskTimeOut
	TaskDone
)

type TaskType int

const (
	MapType = iota
	ReduceType
	WaitingType
	OverType
)

type TaskInfo struct {
}

type Task struct {
	Id         int
	TaskType   TaskType
	Inputfiles []string
	ReduceNum  int
}

// func (t *Task) StartTask() error {
// 	if t.TaksState != TaskWaiting {
// 		return errors.New("启动任务失败,任务状态错误。当前任务状态:" + strconv.Itoa(int(t.TaksState)))
// 	}
// 	t.TaksState = TaskWorking
// 	t.StartTime = time.Now()
// 	return nil
// }

func MakeTask(id int, taskType TaskType, inputfiles []string, reduceNum int) *Task {
	m := Task{
		Id:         id,
		TaskType:   taskType,
		Inputfiles: inputfiles,
		ReduceNum:  reduceNum,
	}
	return &m
}

type TCB struct {
	Id        int
	StartTime time.Time
	TaskType  TaskType
	TaskState TaskState
	Task      *Task
}

func MakeTCB(task *Task) *TCB {
	tcb := TCB{
		Id:        task.Id,
		StartTime: time.Now(),
		TaskType:  task.TaskType,
		TaskState: TaskWaiting,
		Task:      task,
	}
	return &tcb
}

func (t *TCB) StartTask() error {
	if t.TaskState != TaskWaiting {
		return errors.New("Can not start task, because the task is not waiting state!")
	}
	t.StartTime = time.Now()
	t.TaskState = TaskWorking
	return nil
}
